﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Xml;
using Common.Logging;
using In.Api.Dto.HisWs;
using In.Api.Helpers;
using InDbContext;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Configuration;
using System.ServiceModel;

namespace In.Api.Controllers
{
    public class HisWsController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(HisWsController));

        /// <summary>
        /// 获取一级科室信息        
        /// </summary>
        [HttpPost]
        [ActionName("GetDeptGroupInfo")]
        public GetDeptInfoResponse GetDeptGroupInfo(GetDeptInfoRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1014";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;
            var responseXml = "";

            try
            {
                responseXml = wsClient.QueryDepartmentGroup(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetDeptGroupInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            HospWsLogHelper.Log("GetDeptGroupInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetDeptInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.DeptList = new List<GetDeptInfoResponse.DeptInfo>();

            var nodes = xmlDoc.SelectNodes("/Response/Departments/Department");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var deptObj = new GetDeptInfoResponse.DeptInfo();

                    var deptName = xNode.SelectSingleNode("DepartmentName")?.InnerText ?? "";
                    var deptId = xNode.SelectSingleNode("DepartmentCode")?.InnerText ?? "";

                    deptObj.DeptId = deptId;
                    deptObj.DeptName = deptName;

                    response.DeptList.Add(deptObj);
                }
            }
            return response;
        }
        /// <summary>
        /// 获取二级科室信息        
        /// </summary>
        [HttpPost]
        [ActionName("GetDeptInfo")]
        public GetDeptInfoResponse GetDeptInfo(GetDeptInfoRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1012";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode deptIdNode = xmlDoc.CreateElement("DepartmentCode");
            deptIdNode.InnerText = request.DeptId;
            root.AppendChild(deptIdNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;
            var responseXml = "";

            try
            {
                responseXml = wsClient.QueryDepartment(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 getDeptInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            HospWsLogHelper.Log("getDeptInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetDeptInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.DeptList = new List<GetDeptInfoResponse.DeptInfo>();

            var nodes = xmlDoc.SelectNodes("/Response/Departments/Department");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var deptObj = new GetDeptInfoResponse.DeptInfo();

                    var deptName = xNode.SelectSingleNode("DepartmentName")?.InnerText ?? "";
                    var deptId = xNode.SelectSingleNode("DepartmentCode")?.InnerText ?? "";
                    var loc = xNode.SelectSingleNode("DepartmentAddress")?.InnerText ?? "";
                    var parentId = xNode.SelectSingleNode("ParentId")?.InnerText ?? "";
                    var desc = xNode.SelectSingleNode("Description")?.InnerText ?? "";

                    deptObj.DeptId = deptId;
                    deptObj.DeptName = deptName;
                    deptObj.Loc = loc;
                    deptObj.ParentId = parentId;
                    deptObj.Desc = desc;

                    response.DeptList.Add(deptObj);
                }
            }
            return response;
        }

        /// <summary>
        /// 获取医生信息
        /// </summary>
        [HttpPost]
        [ActionName("GetDoctorInfo")]
        public GetDoctorInfoResponse GetDoctorInfo(GetDoctorInfoRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1013";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode departmentCodeNode = xmlDoc.CreateElement("DepartmentCode");
            departmentCodeNode.InnerText = request.DeptId;
            root.AppendChild(departmentCodeNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";

            try
            {
                responseXml = wsClient.QueryDoctor(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 getDoctorInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("getDoctorInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetDoctorInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";


            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.DoctorList = new List<GetDoctorInfoResponse.DoctorInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/Doctors/Doctor");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var doctorInfoObj = new GetDoctorInfoResponse.DoctorInfo();
                    var doctorId = xNode.SelectSingleNode("DoctorCode")?.InnerText ?? "";
                    var doctorName = xNode.SelectSingleNode("DoctorName")?.InnerText ?? "";
                    var deptId = xNode.SelectSingleNode("DeptId")?.InnerText ?? "";
                    var title = xNode.SelectSingleNode("DoctotLevelCode")?.InnerText ?? "";
                    var doctorLevel = xNode.SelectSingleNode("DoctorLevel")?.InnerText ?? "";
                    var deptName = xNode.SelectSingleNode("DeptName")?.InnerText ?? "";

                    doctorInfoObj.DoctorId = doctorId;
                    doctorInfoObj.DoctorName = doctorName;
                    doctorInfoObj.DeptId = deptId;
                    doctorInfoObj.Title = title;
                    doctorInfoObj.DoctorLevel = doctorLevel;
                    doctorInfoObj.DeptName = deptName;

                    response.DoctorList.Add(doctorInfoObj);
                }
            }

            return response;
        }

        /// <summary>
        /// 获取患者信息
        /// </summary>
        [HttpPost]
        [ActionName("GetPatientInfo")]
        public GetPatientInfoResponse GetPatientInfo(GetPatientInfoRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "3300";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);
            xmlDoc.AppendChild(root);

            XmlNode patientCardNode = xmlDoc.CreateElement("PatientCard");
            patientCardNode.InnerText = request.CardNo;
            root.AppendChild(patientCardNode);

            XmlNode PatientIDNode = xmlDoc.CreateElement("PatientID");
            PatientIDNode.InnerText = request.PatientID;
            root.AppendChild(PatientIDNode);

            XmlNode patientNameNode = xmlDoc.CreateElement("PatientName");
            patientNameNode.InnerText = request.PatientName;
            root.AppendChild(patientNameNode);
            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";

            try
            {
                responseXml = wsClient.GetPatInfo(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 getpatientInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("getpatientInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetPatientInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.PatientInfo = new GetPatientInfoResponse.Patient();

            var nodes = xmlDoc.SelectNodes("/Response");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var patientInfoObj = new GetPatientInfoResponse.Patient();
                    var patientId = xNode.SelectSingleNode("PatientID")?.InnerText ?? "";
                    var name = xNode.SelectSingleNode("PatientName")?.InnerText ?? "";
                    var gender = xNode.SelectSingleNode("Sex")?.InnerText ?? "";
                    var birthDate = xNode.SelectSingleNode("DOB")?.InnerText ?? "";
                    var idenNo = xNode.SelectSingleNode("IDNo")?.InnerText ?? "";
                    var phone = xNode.SelectSingleNode("TelephoneNo")?.InnerText ?? "";
                    var feeType = xNode.SelectSingleNode("PatType")?.InnerText ?? "";
                    var cardNo = xNode.SelectSingleNode("PatientCard")?.InnerText ?? "";

                    patientInfoObj.PatientId = patientId;
                    patientInfoObj.Name = name;
                    patientInfoObj.Gender = gender;
                    patientInfoObj.Birthdate = birthDate;
                    patientInfoObj.IdenNo = idenNo;
                    patientInfoObj.Phone = phone;
                    patientInfoObj.FeeType = feeType;
                    patientInfoObj.CardNo = cardNo;

                    response.PatientInfo = patientInfoObj;
                }
            }

            return response;
        }

        /// <summary>
        /// 创建虚拟就诊卡        
        /// </summary>
        [HttpPost]
        [ActionName("CreateCard")]
        public CreateCardResponse CreateCard(CreateCardRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "3014";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode userIDNode = xmlDoc.CreateElement("UserID");
            userIDNode.InnerText = "1";
            root.AppendChild(userIDNode);

            XmlNode idenNo = xmlDoc.CreateElement("IDNo");
            idenNo.InnerText = request.IdenNo;
            root.AppendChild(idenNo);

            XmlNode nameNode = xmlDoc.CreateElement("PatientName");
            nameNode.InnerText = request.Name;
            root.AppendChild(nameNode);

            XmlNode genderNode = xmlDoc.CreateElement("Sex");
            genderNode.InnerText = request.Gender;
            root.AppendChild(genderNode);

            XmlNode birthNode = xmlDoc.CreateElement("DOB");
            birthNode.InnerText = request.Birthdate;
            root.AppendChild(birthNode);

            XmlNode phoneNode = xmlDoc.CreateElement("Mobile");
            phoneNode.InnerText = request.Phone;
            root.AppendChild(phoneNode);

            XmlNode addressNode = xmlDoc.CreateElement("Address");
            addressNode.InnerText = request.Address;
            root.AppendChild(addressNode);

            XmlNode patientTypeNode = xmlDoc.CreateElement("PatientType");
            patientTypeNode.InnerText = "01";
            root.AppendChild(patientTypeNode);

            XmlNode cardNoNode = xmlDoc.CreateElement("PatientCard");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";

            try
            {
                responseXml = wsClient.SavePatientCard(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 CreateCard 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("CreateCard", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new CreateCardResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;

            node = xmlDoc.SelectSingleNode("/Response/PatientCard");
            var cardNo = node?.InnerText ?? "";
            response.CardNo = cardNo;

            node = xmlDoc.SelectSingleNode("/Response/PatientID");
            var patientId = node?.InnerText ?? "";
            response.PatientId = patientId;

            return response;
        }

        /// <summary>
        /// 获取排班信息
        /// </summary>
        [HttpPost]
        [ActionName("GetScheduleInfo")]
        public GetScheduleInfoResponse GetScheduleInfo(GetScheduleInfoRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1004";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode deptIdNode = xmlDoc.CreateElement("DepartmentCode");
            deptIdNode.InnerText = request.DeptId;
            root.AppendChild(deptIdNode);

            XmlNode beginDateNode = xmlDoc.CreateElement("StartDate");
            beginDateNode.InnerText = request.BeginDate;
            root.AppendChild(beginDateNode);

            XmlNode endDateNode = xmlDoc.CreateElement("EndDate");
            endDateNode.InnerText = request.EndDate;
            root.AppendChild(endDateNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";

            try
            {
                responseXml = wsClient.QueryAdmSchedule(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetScheduleInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("GetScheduleInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetScheduleInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.ScheduleList = new List<GetScheduleInfoResponse.ScheduleInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/Schedules/Schedule");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var scheduleItemCode = xNode.SelectSingleNode("ScheduleItemCode")?.InnerText ?? "";
                    var regType = xNode.SelectSingleNode("DoctorSessType")?.InnerText ?? "";
                    var deptName = xNode.SelectSingleNode("DepartmentName")?.InnerText ?? "";
                    var timeSlice = xNode.SelectSingleNode("SessionName")?.InnerText ?? "";
                    var doctorName = xNode.SelectSingleNode("DoctorName")?.InnerText ?? "";
                    var fee = xNode.SelectSingleNode("Fee")?.InnerText ?? "";
                    var treatFee = xNode.SelectSingleNode("CheckupFee")?.InnerText ?? "";
                    var feeValue = fee.ToDecimalOrDefault();
                    var treatFeeValue = treatFee.ToDecimalOrDefault();
                    var deptId = xNode.SelectSingleNode("DepartmentCode")?.InnerText ?? "";
                    var doctorId = xNode.SelectSingleNode("DoctorCode")?.InnerText ?? "";
                    var title = xNode.SelectSingleNode("DoctorTitle")?.InnerText ?? "";
                    var regDate = xNode.SelectSingleNode("ServiceDate")?.InnerText ?? "";
                    var isHalt = xNode.SelectSingleNode("ScheduleStatus")?.InnerText ?? "";
                    var startTime = xNode.SelectSingleNode("StartTime")?.InnerText ?? "";
                    var endTime = xNode.SelectSingleNode("EndTime")?.InnerText ?? "";
                    var totalCount = xNode.SelectSingleNode("AvailableTotalNum")?.InnerText ?? "";
                    var leaveCount = xNode.SelectSingleNode("AvailableLeftNum")?.InnerText ?? "";
                    var isTimeFlag = xNode.SelectSingleNode("TimeRangeFlag")?.InnerText ?? "";
                    var otherFee = xNode.SelectSingleNode("OtherFee")?.InnerText ?? "";

                    // 已经停诊的排班不显示
                    if (isHalt == "S")
                    {
                        continue;
                    }

                    // 已经结束的排班不显示
                    if (string.CompareOrdinal(regDate, DateTime.Now.ToString("yyyy-MM-dd")) < 0
                        ||
                        (string.CompareOrdinal(regDate, DateTime.Now.ToString("yyyy-MM-dd")) == 0
                         && string.CompareOrdinal(endTime, DateTime.Now.ToString("HH:mm")) <= 0))
                    {
                        //continue;
                        totalCount = "1";
                        leaveCount = "0";
                    }

                    #region
                    //手术，急诊，护理，感染号不显示；
                    //2015-02-09 开放护理门诊号   regType.Contains("护理门诊") ||
                    //if (regType.Contains("手术") || regType.Contains("急诊") || timeSlice.Contains("感染")
                    //    || regType.Contains("感染"))
                    //{
                    //    //continue;
                    //    totalCount = "1";
                    //    leaveCount = "0";
                    //}

                    // 挂号费和诊疗费为零的排班不显示，但护理门诊的建档除外                    
                    //if ((feeValue.CompareTo(0.00M) == 0)
                    //    && (treatFeeValue.CompareTo(0.00M) == 0)
                    //    && deptName.IndexOf("护理门诊", StringComparison.Ordinal) < 0
                    //    && doctorName.IndexOf("建档", StringComparison.Ordinal) < 0)
                    //{
                    //    //continue;
                    //    totalCount = "1";
                    //    leaveCount = "0";
                    //}

                    // 不限号的排班只可以做当日挂号，不做预约
                    // 急诊号只可以做当日挂号，不做预约
                    //if ((totalCount == "不限号" || regType.IndexOf("急诊", StringComparison.Ordinal) >= 0) &&
                    //     string.CompareOrdinal(regDate, DateTime.Now.ToString("yyyy-MM-dd")) > 0)
                    //{
                    //    //continue;
                    //    totalCount = "1";
                    //    leaveCount = "0";
                    //}

                    // 限号但没有分时的排班为现场号，不可以在微信上挂号
                    //if (totalCount != "不限号" && isTimeFlag == "0")
                    //{
                    //    //continue;
                    //    totalCount = "1";
                    //    leaveCount = "0";
                    //}

                    // 急诊号只能在排班开始后显示
                    //if (regType.IndexOf("急诊", StringComparison.Ordinal) >= 0
                    //    && string.CompareOrdinal(startTime, DateTime.Now.ToString("HH:mm")) > 0)
                    //{
                    //    //continue;
                    //    totalCount = "1";
                    //    leaveCount = "0";
                    //}
                    #endregion

                    var scheduleObj = new GetScheduleInfoResponse.ScheduleInfo();
                    scheduleObj.ScheduleItemCode = scheduleItemCode;
                    scheduleObj.DeptId = deptId;
                    scheduleObj.DeptName = deptName;
                    scheduleObj.DoctorId = doctorId;
                    scheduleObj.DoctorName = doctorName;
                    scheduleObj.Title = title;
                    scheduleObj.RegDate = regDate;
                    scheduleObj.IsHalt = isHalt;
                    scheduleObj.TimeSlice = timeSlice;
                    scheduleObj.StartTime = startTime;
                    scheduleObj.EndTime = endTime;
                    scheduleObj.TotalCount = totalCount;
                    scheduleObj.LeaveCount = leaveCount;
                    scheduleObj.IsTimeFlag = isTimeFlag;
                    scheduleObj.RegType = regType;
                    scheduleObj.Fee = fee;
                    scheduleObj.TreatFee = treatFee;
                    scheduleObj.OtherFee = otherFee;

                    response.ScheduleList.Add(scheduleObj);
                }

                response.ScheduleList.Sort(new ScheduleInfoComparer());
            }
            return response;
        }

        /// <summary>
        /// 获取排班分时信息(没用)
        /// </summary>
        [HttpPost]
        [ActionName("GetTimeRegInfo")]
        public GetTimeRegInfoResponse GetTimeRegInfo(GetTimeRegInfoRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "10041";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode deptIdNode = xmlDoc.CreateElement("DepartmentCode");
            deptIdNode.InnerText = request.DeptId;
            root.AppendChild(deptIdNode);

            XmlNode doctorIdNode = xmlDoc.CreateElement("DoctorCode");
            doctorIdNode.InnerText = request.DoctorId;
            root.AppendChild(doctorIdNode);

            XmlNode timeSliceNode = xmlDoc.CreateElement("RBASSessionCode");
            timeSliceNode.InnerText = request.TimeSlice;
            root.AppendChild(timeSliceNode);

            XmlNode regTypeNode = xmlDoc.CreateElement("ScheduleItemCode");
            regTypeNode.InnerText = request.RegType;
            root.AppendChild(regTypeNode);

            XmlNode regDateNode = xmlDoc.CreateElement("ServiceDate");
            regDateNode.InnerText = request.RegDate;
            root.AppendChild(regDateNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.QueryScheduleTimeInfo(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 getTimeRegInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("getTimeRegInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetTimeRegInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";


            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.TimeRegList = new List<GetTimeRegInfoResponse.TimeRegInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/TimeRanges/TimeRange");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var timeRegObj = new GetTimeRegInfoResponse.TimeRegInfo();
                    var startTime = xNode.SelectSingleNode("StartTime")?.InnerText ?? "";
                    var endTime = xNode.SelectSingleNode("EndTime")?.InnerText ?? "";
                    var totalCount = xNode.SelectSingleNode("AvailableTotalNum")?.InnerText ?? "";
                    var leaveCount = xNode.SelectSingleNode("AvailableLeftNum")?.InnerText ?? "";
                    var sqNo = xNode.SelectSingleNode("TimeRangeSeqNo")?.InnerText ?? "";

                    timeRegObj.StartTime = startTime;
                    timeRegObj.EndTime = endTime;
                    timeRegObj.TotalCount = totalCount;
                    timeRegObj.LeaveCount = leaveCount;
                    timeRegObj.SqNo = sqNo;

                    response.TimeRegList.Add(timeRegObj);
                }
            }

            return response;
        }

        /// <summary>
        /// 锁定号源
        /// </summary>
        [HttpPost]
        [ActionName("ExeLockSource")]
        public ExeLockSourceResponse ExeLockSource(ExeLockSourceRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "10015";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode orderIdNode = xmlDoc.CreateElement("PayOrderId");
            orderIdNode.InnerText = request.OrderId;
            root.AppendChild(orderIdNode);

            XmlNode regDateNode = xmlDoc.CreateElement("TradeDate");
            regDateNode.InnerText = request.RegDate;
            root.AppendChild(regDateNode);


            XmlNode regTypeNode = xmlDoc.CreateElement("ScheduleItemCode");
            regTypeNode.InnerText = request.RegType;
            root.AppendChild(regTypeNode);

            XmlNode cardNoNode = xmlDoc.CreateElement("PatientID");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);

            XmlNode cardTypeNode = xmlDoc.CreateElement("CardType");
            cardTypeNode.InnerText = request.CardType;
            root.AppendChild(cardTypeNode);


            XmlNode startTimeNode = xmlDoc.CreateElement("BeginTime");
            startTimeNode.InnerText = request.StartTime;
            root.AppendChild(startTimeNode);

            XmlNode endTimeNode = xmlDoc.CreateElement("EndTime");
            endTimeNode.InnerText = request.EndTime;
            root.AppendChild(endTimeNode);

            XmlNode sqNoNode = xmlDoc.CreateElement("LockQueueNo");//锁号可不填
            sqNoNode.InnerText = request.SqNo;
            root.AppendChild(sqNoNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.LockOrder(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 ExeLockSource 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("ExeLockSource", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new ExeLockSourceResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;

            var lockQueueNo = node.SelectSingleNode("/Response/LockQueueNo")?.InnerText ?? "";
            response.LockQueueNo = lockQueueNo;

            var scheduleItemCode = node.SelectSingleNode("/Response/ScheduleItemCode")?.InnerText ?? "";
            response.ScheduleItemCode = scheduleItemCode;

            var admDoc = node.SelectSingleNode("/Response/AdmDoc")?.InnerText ?? "";
            response.AdmDoc = admDoc;

            var admDate = node.SelectSingleNode("/Response/AdmDate")?.InnerText ?? "";
            response.AdmDate = admDate;

            var admTime = node.SelectSingleNode("/Response/AdmTime")?.InnerText ?? "";
            response.AdmTime = admTime;

            var regFee = node.SelectSingleNode("/Response/RegFee")?.InnerText ?? "";
            response.RegFee = regFee;

            return response;
        }


        /// <summary>
        /// 解锁号源
        /// </summary>
        [HttpPost]
        [ActionName("ExeUnLockSource")]
        public ExeLockSourceResponse ExeUnLockSource(ExeLockSourceRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "10016";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode orderIdNode = xmlDoc.CreateElement("PayOrderId");
            orderIdNode.InnerText = request.OrderId;
            root.AppendChild(orderIdNode);

            XmlNode regDateNode = xmlDoc.CreateElement("TradeDate");
            regDateNode.InnerText = request.RegDate;
            root.AppendChild(regDateNode);


            XmlNode regTypeNode = xmlDoc.CreateElement("ScheduleItemCode");
            regTypeNode.InnerText = request.RegType;
            root.AppendChild(regTypeNode);

            XmlNode cardNoNode = xmlDoc.CreateElement("PatientID");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);

            XmlNode cardTypeNode = xmlDoc.CreateElement("CardType");
            cardTypeNode.InnerText = request.CardType;
            root.AppendChild(cardTypeNode);



            XmlNode startTimeNode = xmlDoc.CreateElement("BeginTime");
            startTimeNode.InnerText = request.StartTime;
            root.AppendChild(startTimeNode);

            XmlNode endTimeNode = xmlDoc.CreateElement("EndTime");
            endTimeNode.InnerText = request.EndTime;
            root.AppendChild(endTimeNode);

            XmlNode sqNoNode = xmlDoc.CreateElement("LockQueueNo");//解锁必填
            sqNoNode.InnerText = request.LockQueueNo;
            root.AppendChild(sqNoNode);

            XmlNode oldTransactionIdNode = xmlDoc.CreateElement("OldTransactionId");
            oldTransactionIdNode.InnerText = request.OldTransactionId;
            root.AppendChild(oldTransactionIdNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.LockOrder(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 ExeLockSource 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("ExeLockSource", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new ExeLockSourceResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;

            return response;
        }



        /// <summary>
        /// 预约挂号
        /// </summary>
        [HttpPost]
        [ActionName("ExeAppoint")]
        public ExeAppointResponse ExeAppoint(ExeAppointRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1101";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode transactionIdNode = xmlDoc.CreateElement("TransactionId");
            transactionIdNode.InnerText = request.TransactionId;
            root.AppendChild(transactionIdNode);

            XmlNode scheduleItemCodeNode = xmlDoc.CreateElement("ScheduleItemCode");
            scheduleItemCodeNode.InnerText = request.ScheduleItemCode;
            root.AppendChild(scheduleItemCodeNode);

            XmlNode cardNoNode = xmlDoc.CreateElement("PatientCard");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);

            XmlNode cardTypeNode = xmlDoc.CreateElement("CardType");
            cardTypeNode.InnerText = request.CardType;
            root.AppendChild(cardTypeNode);

            XmlNode payFeeNode = xmlDoc.CreateElement("PayFee");
            payFeeNode.InnerText = request.PayFee;
            root.AppendChild(payFeeNode);

            XmlNode payModeCodeNode = xmlDoc.CreateElement("PayModeCode");
            payModeCodeNode.InnerText = "WX";
            root.AppendChild(payModeCodeNode);

            XmlNode queueNoNode = xmlDoc.CreateElement("QueueNo");
            queueNoNode.InnerText = request.QueueNo;
            root.AppendChild(queueNoNode);

            XmlNode payOrderIdNode = xmlDoc.CreateElement("PayOrderId");
            payOrderIdNode.InnerText = request.OrderId;
            root.AppendChild(payOrderIdNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.OPRegister(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 ExeAppoint 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("ExeAppoint", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new ExeAppointResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;

            var seqCode = node.SelectSingleNode("/Response/SeqCode")?.InnerText ?? "";
            response.SeqCode = seqCode;

            var admitAddress = node.SelectSingleNode("/Response/AdmitAddress")?.InnerText ?? "";
            response.AdmitAddress = admitAddress;

            var admNo = node.SelectSingleNode("/Response/AdmNo")?.InnerText ?? "";
            response.AdmNo = admNo;

            return response;
        }

        /// <summary>
        /// 取消预约
        /// </summary>
        [HttpPost]
        [ActionName("CancelAppoint")]
        public CancelAppointResponse CancelAppoint(CancelAppointRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1103";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode admNoNode = xmlDoc.CreateElement("AdmNo");
            admNoNode.InnerText = request.AdmNo;
            root.AppendChild(admNoNode);

            XmlNode transactionIdNode = xmlDoc.CreateElement("TransactionId");
            transactionIdNode.InnerText = request.TransactionId;
            root.AppendChild(transactionIdNode);

            XmlNode OrderIdNode = xmlDoc.CreateElement("PayOrderId");
            OrderIdNode.InnerText = request.OrderId;
            root.AppendChild(OrderIdNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.OPRegReturn(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 OPRegReturn 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("OPRegReturn", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new CancelAppointResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ReturnFee");
            var returnFee = node?.InnerText ?? "";

            //node = xmlDoc.SelectSingleNode("/Response/HisTradeNo");
            //var hisTradeNo = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.Fee = returnFee;
            //response.HisTradeNo = hisTradeNo;

            return response;
        }

        /// <summary>
        /// 获取挂号记录
        /// </summary>
        /// <param name="request"></param>
        [HttpPost]
        [ActionName("QueryAdmOPReg")]
        public Response4QueryAdmOPReg QueryAdmOPReg(Request4QueryAdmOPReg request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1104";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);


            XmlNode patientCardNode = xmlDoc.CreateElement("PatientCard");
            patientCardNode.InnerText = request.CardNo;
            root.AppendChild(patientCardNode);

            XmlNode cardTypeNode = xmlDoc.CreateElement("CardType");
            cardTypeNode.InnerText = request.CardType;
            root.AppendChild(cardTypeNode);

            XmlNode patientIDNode = xmlDoc.CreateElement("PatientID");
            patientIDNode.InnerText = request.PatientID;
            root.AppendChild(patientIDNode);

            XmlNode payOrderIdNode = xmlDoc.CreateElement("PayOrderId");
            payOrderIdNode.InnerText = request.OrderId;
            root.AppendChild(payOrderIdNode);

            XmlNode admNoNode = xmlDoc.CreateElement("AdmNo");
            admNoNode.InnerText = request.AdmNo;
            root.AppendChild(admNoNode);

            XmlNode startDateNode = xmlDoc.CreateElement("StartDate");
            startDateNode.InnerText = request.StartDate;
            root.AppendChild(startDateNode);

            XmlNode endDateNode = xmlDoc.CreateElement("EndDate");
            endDateNode.InnerText = request.EndDate;
            root.AppendChild(endDateNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.QueryAdmOPReg(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 QueryAdmOPReg 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("QueryAdmOPReg", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var responseObj = new Response4QueryAdmOPReg();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node == null ? "" : node.InnerText;

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node == null ? "" : node.InnerText;
            if (resultCode != "0")
            {
                responseObj.ResultCode = resultCode;
                responseObj.ResultDesc = resultDesc;
                return responseObj;
            }

            responseObj.ResultCode = resultCode;
            responseObj.ResultDesc = resultDesc;

            responseObj.AdmOPReg = new List<Response4QueryAdmOPReg.Response4QueryAdmOPRegInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/Orders/Order");
            foreach (XmlNode xNode in nodes)
            {
                var haltObj = new Response4QueryAdmOPReg.Response4QueryAdmOPRegInfo();

                var status = xNode.SelectSingleNode("Status") == null ? "" : xNode.SelectSingleNode("Status").InnerText.Trim();
                var regID = xNode.SelectSingleNode("RegID") == null ? "" : xNode.SelectSingleNode("RegID").InnerText.Trim();
                var regDate = xNode.SelectSingleNode("RegDate") == null ? "" : xNode.SelectSingleNode("RegDate").InnerText.Trim();
                var patName = xNode.SelectSingleNode("PatName") == null ? "" : xNode.SelectSingleNode("PatName").InnerText.Trim();
                var patientID = xNode.SelectSingleNode("PatientID") == null ? "" : xNode.SelectSingleNode("PatientID").InnerText.Trim();
                var admitDate = xNode.SelectSingleNode("AdmitDate") == null ? "" : xNode.SelectSingleNode("AdmitDate").InnerText.Trim();
                var hospitalName = xNode.SelectSingleNode("HospitalName") == null ? "" : xNode.SelectSingleNode("HospitalName").InnerText.Trim();
                var department = xNode.SelectSingleNode("Department") == null ? "" : xNode.SelectSingleNode("Department").InnerText.Trim();
                var doctor = xNode.SelectSingleNode("Doctor") == null ? "" : xNode.SelectSingleNode("Doctor").InnerText.Trim();
                var doctorTitle = xNode.SelectSingleNode("DoctorTitle") == null ? "" : xNode.SelectSingleNode("DoctorTitle").InnerText.Trim();
                var regFee = xNode.SelectSingleNode("RegFee") == null ? "" : xNode.SelectSingleNode("RegFee").InnerText.Trim();
                var seqCode = xNode.SelectSingleNode("SeqCode") == null ? "" : xNode.SelectSingleNode("SeqCode").InnerText.Trim();
                var sessionName = xNode.SelectSingleNode("SessionName") == null ? "" : xNode.SelectSingleNode("SessionName").InnerText.Trim();
                var admitAddress = xNode.SelectSingleNode("AdmitAddress") == null ? "" : xNode.SelectSingleNode("AdmitAddress").InnerText.Trim();
                var admitRange = xNode.SelectSingleNode("AdmitRange") == null ? "" : xNode.SelectSingleNode("AdmitRange").InnerText.Trim();
                var returnFlag = xNode.SelectSingleNode("ReturnFlag") == null ? "" : xNode.SelectSingleNode("ReturnFlag").InnerText.Trim();

                haltObj.Status = status;
                haltObj.RegID = regID;
                haltObj.RegDate = regDate;
                haltObj.PatName = patName;
                haltObj.PatientID = patientID;
                haltObj.AdmitDate = admitDate;
                haltObj.HospitalName = hospitalName;

                haltObj.Department = department;
                haltObj.Doctor = doctor;
                haltObj.RegFee = regFee;
                haltObj.SeqCode = seqCode;
                haltObj.SessionName = sessionName;
                haltObj.AdmitAddress = admitAddress;
                haltObj.AdmitRange = admitRange;
                haltObj.ReturnFlag = returnFlag;

                responseObj.AdmOPReg.Add(haltObj);
            }
            return responseObj;
        }
        /// <summary>
        /// 获取停诊信息
        /// </summary>
        /// <param name="request"></param>
        [HttpPost]
        [ActionName("GetHaltInfo")]
        public Response4GetHaltInfo GetHaltInfo(Request4GetHaltInfo request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1107";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode startDateNode = xmlDoc.CreateElement("StartDate");
            startDateNode.InnerText = request.StartDate;
            root.AppendChild(startDateNode);

            XmlNode endDateNode = xmlDoc.CreateElement("EndDate");
            endDateNode.InnerText = request.EndDate;
            root.AppendChild(endDateNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.QueryStopDoctorInfo(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 QueryStopDoctorInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("QueryStopDoctorInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var responseObj = new Response4GetHaltInfo();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node == null ? "" : node.InnerText;

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node == null ? "" : node.InnerText;

            if (resultCode == "0")
            {
                responseObj.ResultCode = resultCode;
                responseObj.ResultDesc = resultDesc;

                responseObj.HaltList = new List<Response4GetHaltInfoSubHaltInfo>();
                var nodes = xmlDoc.SelectNodes("/Response/haltList/haltInfo");
                foreach (XmlNode xNode in nodes)
                {
                    var haltObj = new Response4GetHaltInfoSubHaltInfo();

                    var scheduleId = xNode.SelectSingleNode("ScheduleCode") == null ? "" : xNode.SelectSingleNode("ScheduleCode").InnerText.Trim();
                    var doctorId = xNode.SelectSingleNode("DoctorCode") == null ? "" : xNode.SelectSingleNode("DoctorCode").InnerText.Trim();
                    var doctorName = xNode.SelectSingleNode("DoctorName") == null ? "" : xNode.SelectSingleNode("DoctorName").InnerText.Trim();
                    var regDate = xNode.SelectSingleNode("ServiceDate") == null ? "" : xNode.SelectSingleNode("ServiceDate").InnerText.Trim();
                    var timeSlice = xNode.SelectSingleNode("TimeRangeName") == null ? "" : xNode.SelectSingleNode("TimeRangeName").InnerText.Trim();
                    var startTime = xNode.SelectSingleNode("StartTime") == null ? "" : xNode.SelectSingleNode("StartTime").InnerText.Trim();
                    var endTime = xNode.SelectSingleNode("EndTime") == null ? "" : xNode.SelectSingleNode("EndTime").InnerText.Trim();

                    haltObj.ScheduleId = scheduleId;
                    haltObj.DoctorId = doctorId;
                    haltObj.DoctorName = doctorName;
                    haltObj.RegDate = regDate;
                    haltObj.TimeSlice = timeSlice;
                    haltObj.StartTime = startTime;
                    haltObj.EndTime = endTime;

                    responseObj.HaltList.Add(haltObj);
                }
            }
            return responseObj;
        }

        #region
        /// <summary>
        /// 获取订单状态
        /// </summary>
        [HttpPost]
        [ActionName("GetOrderStatus")]
        public GetOrderStatusResponse GetOrderStatus(GetOrderStatusRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode hospitalIdNode = xmlDoc.CreateElement("HospitalID");
            hospitalIdNode.InnerText = "GDSJNKYY";
            root.AppendChild(hospitalIdNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "1104";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("ExtUserID");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode orderIdNode = xmlDoc.CreateElement("PayOrdId");
            orderIdNode.InnerText = request.OrderId;
            root.AppendChild(orderIdNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.QueryRegStatus(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetOrderStatus 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("GetOrderStatus", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetOrderStatusResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node == null ? "" : node.InnerText;

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node == null ? "" : node.InnerText;

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            var xNode = xmlDoc.SelectSingleNode("/Response");
            if (xNode != null)
            {
                var status = xNode.SelectSingleNode("Status")?.InnerText ?? "";
                var seqCode = xNode.SelectSingleNode("SeqCode")?.InnerText ?? "";
                var regFee = xNode.SelectSingleNode("RegFee")?.InnerText ?? "";
                var admitRange = xNode.SelectSingleNode("AdmitRange")?.InnerText ?? "";
                var address = xNode.SelectSingleNode("AdmitAddress")?.InnerText ?? "";
                var transactionId = xNode.SelectSingleNode("TransactionId")?.InnerText ?? "";
                var payOrderId = xNode.SelectSingleNode("PayOrderId")?.InnerText ?? "";
                var admNo = xNode.SelectSingleNode("AdmNo")?.InnerText ?? "";

                response.Status = status;
                response.SeqCode = seqCode;
                response.RegFee = regFee;
                response.AdmitRange = admitRange;
                response.TransactionId = transactionId;
                response.PayOrderId = payOrderId;
                response.Address = address;
                response.AdmNo = admNo;
            }

            return response;
        }

        ///// <summary>
        ///// 获取用户队列信息
        ///// </summary>
        //[HttpPost]
        //[ActionName("GetQueueInfo")]
        //public GetQueueInfoResponse GetQueueInfo(GetQueueInfoRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode queueDateNode = xmlDoc.CreateElement("QueueDate");
        //    queueDateNode.InnerText = request.QueueDate;
        //    root.AppendChild(queueDateNode);

        //    XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
        //    cardNoNode.InnerText = request.CardNo;
        //    root.AppendChild(cardNoNode);

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    var responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.GetQueueInfo(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 GetQueueInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("GetQueueInfo", requestXml, responseXml);

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var response = new GetQueueInfoResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    if (resultCode != "0")
        //    {
        //        response.ResultCode = resultCode;
        //        response.ResultDesc = resultDesc;
        //        return response;
        //    }

        //    response.ResultCode = resultCode;
        //    response.ResultDesc = resultDesc;
        //    response.QueueList = new List<GetQueueInfoResponse.QueueInfo>();
        //    var nodes = xmlDoc.SelectNodes("/Response/Queuelist/QueueInfo");
        //    if (nodes != null)
        //    {
        //        foreach (XmlNode xNode in nodes)
        //        {
        //            var queueInfoObj = new GetQueueInfoResponse.QueueInfo();
        //            var name = xNode.SelectSingleNode("Name")?.InnerText ?? "";
        //            var state = xNode.SelectSingleNode("State")?.InnerText ?? "";
        //            var deptId = xNode.SelectSingleNode("DeptId")?.InnerText ?? "";
        //            var deptName = xNode.SelectSingleNode("DeptName")?.InnerText ?? "";
        //            var doctorId = xNode.SelectSingleNode("DoctorId")?.InnerText ?? "";
        //            var doctorName = xNode.SelectSingleNode("DcotorName")?.InnerText ?? "";
        //            var patientName = xNode.SelectSingleNode("PatientName")?.InnerText ?? "";
        //            var regNo = xNode.SelectSingleNode("RegNo")?.InnerText ?? "";
        //            var foreNum = xNode.SelectSingleNode("ForeNum")?.InnerText ?? "";
        //            var unpayNum = xNode.SelectSingleNode("UnPayNum")?.InnerText ?? "";
        //            var cardNo = xNode.SelectSingleNode("CardNo")?.InnerText ?? "";
        //            var regType = xNode.SelectSingleNode("RegType")?.InnerText ?? "";

        //            queueInfoObj.Name = name;
        //            queueInfoObj.State = state;
        //            queueInfoObj.DeptId = deptId;
        //            queueInfoObj.DeptName = deptName;
        //            queueInfoObj.DoctorId = doctorId;
        //            queueInfoObj.DoctorName = doctorName;
        //            queueInfoObj.PatientName = patientName;
        //            queueInfoObj.RegNo = regNo;
        //            queueInfoObj.ForeNum = foreNum;
        //            queueInfoObj.UnPayNum = unpayNum;
        //            queueInfoObj.CardNo = cardNo;
        //            queueInfoObj.RegType = regType;

        //            response.QueueList.Add(queueInfoObj);
        //        }
        //    }
        //    return response;
        //}

        /// <summary>
        /// 获取缴费单列表
        /// </summary>
        [HttpPost]
        [ActionName("GetPrescriptionList")]
        public GetPrescriptionListResponse GetPrescriptionList(GetPrescriptionListRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);

            XmlNode patientIDNode = xmlDoc.CreateElement("PatientID");
            patientIDNode.InnerText = request.PatientID;
            root.AppendChild(patientIDNode);

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "4902";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("Userid");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode startDateNode = xmlDoc.CreateElement("StartDate");
            startDateNode.InnerText = request.StartDate;
            root.AppendChild(startDateNode);

            XmlNode endDateNode = xmlDoc.CreateElement("EndDate");
            endDateNode.InnerText = request.EndDate;
            root.AppendChild(endDateNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.GetAdmByCardNo(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetPrescriptionList 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("GetPrescriptionList", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            //xmlDoc.LoadXml("<Response><ResultCode>0</ResultCode><ResultContent>获取就诊记录成功</ResultContent><AdmItem><AdmRowid>524240</AdmRowid><AdmDate>2017-10-17</AdmDate><AdmLoc>肿瘤综合治疗中心门诊</AdmLoc><AdmDoctor>山常国</AdmDoctor><AdmLocCode>肿瘤综合治疗中心门诊</AdmLocCode><AdmDoctorCode>1023</AdmDoctorCode><AdmAmt>41.86</AdmAmt><AdmReasonCode>1</AdmReasonCode><AdmReasonDesc>自费</AdmReasonDesc></AdmItem></Response>");
            xmlDoc.LoadXml(responseXml);

            var response = new GetPrescriptionListResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.PrescList = new List<GetPrescriptionListResponse.PrescInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/AdmItem");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var prescInfoObj = new GetPrescriptionListResponse.PrescInfo();
                    var clinicNo = xNode.SelectSingleNode("AdmRowid")?.InnerText ?? "";
                    var prescMoney = xNode.SelectSingleNode("AdmAmt")?.InnerText ?? "";
                    var orderDept = xNode.SelectSingleNode("AdmLoc")?.InnerText ?? "";
                    var orderDate = xNode.SelectSingleNode("AdmDate")?.InnerText ?? "";
                    var orderDoctor = xNode.SelectSingleNode("AdmDoctor")?.InnerText ?? "";

                    prescInfoObj.ClinicNo = clinicNo;
                    prescInfoObj.PrescMoney = prescMoney;
                    prescInfoObj.OrderDept = orderDept;
                    prescInfoObj.OrderDate = orderDate;
                    prescInfoObj.OrderDoctor = orderDoctor;

                    response.PrescList.Add(prescInfoObj);
                }
            }

            return response;
        }

        /// <summary>
        /// 获取缴费单内容（明细）
        /// </summary>
        [HttpPost]
        [ActionName("GetPrescriptionDetail")]
        public GetPrescriptionDetailResponse GetPrescriptionDetail(GetPrescriptionDetailRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "4903";
            root.AppendChild(tradeCodeNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("Userid");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);

            XmlNode prescNoNode = xmlDoc.CreateElement("AdmInfo");
            prescNoNode.InnerText = request.PrescNo;
            root.AppendChild(prescNoNode);

            XmlNode patientIDNode = xmlDoc.CreateElement("PatientID");
            patientIDNode.InnerText = request.PatientID;
            root.AppendChild(patientIDNode);

            XmlNode startDateNode = xmlDoc.CreateElement("StartDate");
            startDateNode.InnerText = request.StartDate;
            root.AppendChild(startDateNode);

            XmlNode endDateNode = xmlDoc.CreateElement("EndDate");
            endDateNode.InnerText = request.EndDate;
            root.AppendChild(endDateNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.GetBillInfo(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetPrescriptionDetail 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }
            
            HospWsLogHelper.Log("GetPrescriptionDetail", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            //xmlDoc.LoadXml("<Response><ResultCode>0</ResultCode><ResultContent>获取费用信息成功.</ResultContent><AdmReasonCount>1</AdmReasonCount><AdmReasons><AdmReason><InsuTypeDesc>自费</InsuTypeDesc><InsuTypeDR>1</InsuTypeDR><AmtSum>40.33</AmtSum><TarOCCateItems><TarOCCateItem><TarOCCateDesc>西药</TarOCCateDesc><TarOCCateAmt>40.33</TarOCCateAmt><OEOrdItems><OEOrdItem><InsuType></InsuType><OEOrdId></OEOrdId><ArcmiDesc>维生素B4片[10mg*100]</ArcmiDesc><Price>.11</Price><Qty>3</Qty><UOM>片</UOM><TotalAmount>.33</TotalAmount><PatientShareAmt>0</PatientShareAmt><DiscAmount>0</DiscAmount></OEOrdItem><OEOrdItem><InsuType></InsuType><OEOrdId></OEOrdId><ArcmiDesc>维生素C泡腾片[1g*18]</ArcmiDesc><Price>40</Price><Qty>1</Qty><UOM>盒(18)</UOM><TotalAmount>40</TotalAmount><PatientShareAmt>0</PatientShareAmt><DiscAmount>0</DiscAmount></OEOrdItem></OEOrdItems></TarOCCateItem></TarOCCateItems></AdmReason></AdmReasons></Response>");
            xmlDoc.LoadXml(responseXml);
            var response = new GetPrescriptionDetailResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.AdmReasonList = new List<GetPrescriptionDetailResponse.AdmReasonInfo>();
            
            var AdmReasons = xmlDoc.SelectNodes("/Response/AdmReasons/AdmReason");
            if (AdmReasons != null)
            {
                foreach (XmlNode AdmReason in AdmReasons)
                {
                    var admReasonInfoObj = new GetPrescriptionDetailResponse.AdmReasonInfo();
                    admReasonInfoObj.PrescItemList = new List<GetPrescriptionDetailResponse.AdmReasonInfo.PrescItemInfo>();
                    var InsuTypeDesc = AdmReason.SelectSingleNode("InsuTypeDesc")?.InnerText ?? "";
                    var InsuTypeDR = AdmReason.SelectSingleNode("InsuTypeDR")?.InnerText ?? "";
                    var AmtSum = AdmReason.SelectSingleNode("AmtSum")?.InnerText ?? "";
                    var TarOCCateItems = AdmReason.SelectNodes("TarOCCateItems/TarOCCateItem");

                    foreach (XmlNode TarOCCateItem in TarOCCateItems)
                    {
                        var prescItemInfoObj = new GetPrescriptionDetailResponse.AdmReasonInfo.PrescItemInfo();
                        var TarOCCateDesc = TarOCCateItem.SelectSingleNode("TarOCCateDesc")?.InnerText ?? "";
                        var TarOCCateAmt = TarOCCateItem.SelectSingleNode("TarOCCateAmt")?.InnerText ?? "";
                       
                        var OEOrdItems = TarOCCateItem.SelectNodes("OEOrdItems/OEOrdItem");
                        foreach (XmlNode OEOrdItem in OEOrdItems)
                        {
                            var OEOrdItemInfoObj = new GetPrescriptionDetailResponse.AdmReasonInfo.PrescItemInfo.OEOrdItemInfo();
                            prescItemInfoObj.OEOrdItemList = new List<GetPrescriptionDetailResponse.AdmReasonInfo.PrescItemInfo.OEOrdItemInfo>();
                            var InsuType = OEOrdItem.SelectSingleNode("InsuType")?.InnerText ?? "";
                            var OEOrdId = OEOrdItem.SelectSingleNode("OEOrdId")?.InnerText ?? "";
                            var ArcmiDesc = OEOrdItem.SelectSingleNode("ArcmiDesc")?.InnerText ?? "";
                            var Price = OEOrdItem.SelectSingleNode("Price")?.InnerText ?? "";
                            var Qty = OEOrdItem.SelectSingleNode("Qty")?.InnerText ?? "";
                            var UOM = OEOrdItem.SelectSingleNode("UOM")?.InnerText ?? "";
                            var TotalAmount = OEOrdItem.SelectSingleNode("TotalAmount")?.InnerText ?? "";
                            var PatientShareAmt = OEOrdItem.SelectSingleNode("PatientShareAmt")?.InnerText ?? "";
                            var DiscAmount = OEOrdItem.SelectSingleNode("DiscAmount")?.InnerText ?? "";

                            OEOrdItemInfoObj.NameDesc = ArcmiDesc;
                            OEOrdItemInfoObj.ItemPrice = Price;
                            OEOrdItemInfoObj.Unit = UOM;
                            OEOrdItemInfoObj.Qty = Qty;
                            OEOrdItemInfoObj.Account = TotalAmount;
                            prescItemInfoObj.OEOrdItemList.Add(OEOrdItemInfoObj);

                        }
                        prescItemInfoObj.ItemName = TarOCCateDesc;
                        prescItemInfoObj.Money = TarOCCateAmt;
                        admReasonInfoObj.PrescItemList.Add(prescItemInfoObj);
                    }

                    admReasonInfoObj.InsuTypeDR = InsuTypeDR;
                    admReasonInfoObj.PrescMoney = AmtSum;
                    admReasonInfoObj.Type = InsuTypeDesc;
                    response.AdmReasonList.Add(admReasonInfoObj);
                }
            }
          
            return response;
        }

        /// <summary>
        /// 缴费
        /// </summary>
        [HttpPost]
        [ActionName("ExePrescriptionPay")]
        public ExePrescriptionPayResponse ExePrescriptionPay(ExePrescriptionPayRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "4904";
            root.AppendChild(tradeCodeNode);

            XmlNode patientIDNode = xmlDoc.CreateElement("PatientID");
            patientIDNode.InnerText = request.PatientID;
            root.AppendChild(patientIDNode);

            XmlNode extUserIDNode = xmlDoc.CreateElement("Userid");
            extUserIDNode.InnerText = "cashier";
            root.AppendChild(extUserIDNode);

            XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);

            XmlNode admInfoNode = xmlDoc.CreateElement("AdmInfo");
            admInfoNode.InnerText = request.AdmInfo;
            root.AppendChild(admInfoNode);

            XmlNode sumFeeNode = xmlDoc.CreateElement("PatAmt");
            sumFeeNode.InnerText = request.SumFee;
            root.AppendChild(sumFeeNode);

            //XmlNode bankTradeInfoNode = xmlDoc.CreateElement("BankTradeInfo");
            //bankTradeInfoNode.InnerXml = "<Response><TradeCode></TradeCode><BankCode></BankCode><BankDate>" + request.TransTime + "</BankDate><BankTradeNo>" + request.TransNo + "</BankTradeNo><ResultCode></ResultCode><ResultContent></ResultContent><PayCardNo></PayCardNo><BankAccDate></BankAccDate><RevTranFlag></RevTranFlag><PatientID></PatientID><PayAmt></PayAmt><OrgHISTradeNo></OrgHISTradeNo><OrgPaySeqNo></OrgPaySeqNo></Response>";
            //root.AppendChild(bankTradeInfoNode);

            XmlNode insuTypeDRNode = xmlDoc.CreateElement("InsuTypeDR");
            insuTypeDRNode.InnerText = request.InsuTypeDR;
            root.AppendChild(insuTypeDRNode);

            XmlNode payModeCodeDRNode = xmlDoc.CreateElement("PayModeCode");
            payModeCodeDRNode.InnerText = "WX";
            root.AppendChild(payModeCodeDRNode);

            XmlNode expStrNode = xmlDoc.CreateElement("ExpStr");
            expStrNode.InnerText = "2";
            root.AppendChild(expStrNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var bankTradeInfo = "<BankTradeInfo><![CDATA[<Response><TradeCode></TradeCode><BankCode></BankCode><BankDate>" + request.TransTime + "</BankDate><BankTradeNo>" + request.TransNo + "</BankTradeNo><ResultCode>0000</ResultCode><ResultContent></ResultContent><PayCardNo></PayCardNo><BankAccDate>" + request.TransTime + "</BankAccDate><RevTranFlag>0</RevTranFlag><PatientID>" + request.PatientID+"</PatientID><PayAmt>"+ request.SumFee + "</PayAmt><OrgHISTradeNo></OrgHISTradeNo><OrgPaySeqNo></OrgPaySeqNo></Response>]]></BankTradeInfo></Request>";
            requestXml= requestXml.Replace("</Request>", bankTradeInfo);
            
            var responseXml = "";
            try
            {
                responseXml = wsClient.AutoOPBillCharge(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 ExePrescriptionPay 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("ExePrescriptionPay", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            //xmlDoc.LoadXml("<Response><ResultCode>0</ResultCode><ResultContent>结算成功</ResultContent><Invoices><Invoice d5p1:type='Invoice' xmlns:d5p1='http://www.w3.org/2001/XMLSchema-instance'><InvoiceNO>835175</InvoiceNO><InvoiceAmt>6.40</InvoiceAmt></Invoice></Invoices></Response>");

            xmlDoc.LoadXml(responseXml);

            var response = new ExePrescriptionPayResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.InvoiceList = new List<ExePrescriptionPayResponse.Invoice>();
            var nodes = xmlDoc.SelectNodes("/Response/Invoices/Invoice");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var disWinInfoObj = new ExePrescriptionPayResponse.Invoice();
                    var InvoiceNO = xNode.SelectSingleNode("InvoiceNO")?.InnerText ?? "";
                    var InvoiceAmt = xNode.SelectSingleNode("InvoiceAmt ")?.InnerText ?? "";
                   
                    disWinInfoObj.InvoiceNO = InvoiceNO;
                    disWinInfoObj.InvoiceAmt = InvoiceAmt;
                    response.InvoiceList.Add(disWinInfoObj);
                }
            }

            return response;
        }

        /// <summary>
        /// 已缴费记录查询
        /// </summary>
        [HttpPost]
        [ActionName("GetCompletedPayInfo")]
        public GetCompletedPayInfoResponse GetCompletedPayInfo(GetCompletedPayInfoRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "4905";
            root.AppendChild(tradeCodeNode);

            XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);
           
            XmlNode patientIdNode = xmlDoc.CreateElement("PatientID");
            patientIdNode.InnerText = request.PatientId;
            root.AppendChild(patientIdNode);

            XmlNode startDateNode = xmlDoc.CreateElement("StartDate");
            startDateNode.InnerText = request.StartDate;
            root.AppendChild(startDateNode);

            XmlNode endDateNode = xmlDoc.CreateElement("EndDate");
            endDateNode.InnerText = request.EndDate;
            root.AppendChild(endDateNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.GetCompletedPayInfo(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetCompletedPayInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("GetCompletedPayInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetCompletedPayInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.PayInfo = new List<GetCompletedPayInfoResponse.CompletedPayInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/PayListInfos/PayListInfo");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var prescInfoObj = new GetCompletedPayInfoResponse.CompletedPayInfo();
                    var Adm = xNode.SelectSingleNode("Adm")?.InnerText ?? "";
                    var AdmDate = xNode.SelectSingleNode("AdmDate")?.InnerText ?? "";
                    var DoctorName = xNode.SelectSingleNode("DoctorName")?.InnerText ?? "";
                    var AdmDepName = xNode.SelectSingleNode("AdmDepName")?.InnerText ?? "";
                    var PayAmout = xNode.SelectSingleNode("PayAmout")?.InnerText ?? "";
                    var ChargeDate = xNode.SelectSingleNode("ChargeDate")?.InnerText ?? "";
                    var ReceiptId = xNode.SelectSingleNode("ReceiptId")?.InnerText ?? "";

                    prescInfoObj.Adm = Adm;
                    prescInfoObj.AdmDate = AdmDate;
                    prescInfoObj.DoctorName = DoctorName;
                    prescInfoObj.AdmDepName = AdmDepName;
                    prescInfoObj.PayAmout = PayAmout;
                    prescInfoObj.ReceiptId = ReceiptId;
                    prescInfoObj.ChargeDate = ChargeDate;

                    response.PayInfo.Add(prescInfoObj);
                }
            }

            return response;
        }

        /// <summary>
        /// 已缴费记录明细查询
        /// </summary>
        [HttpPost]
        [ActionName("GetCompletedPayDetailInfo")]
        public GetCompletedPayDetailInfoResponse GetCompletedPayDetailInfo(GetCompletedPayDetailInfoRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "4906";
            root.AppendChild(tradeCodeNode);

            XmlNode admInfoNode = xmlDoc.CreateElement("AdmInfo");
            admInfoNode.InnerText = request.AdmInfo;
            root.AppendChild(admInfoNode);

            XmlNode receiptIdNode = xmlDoc.CreateElement("ReceiptId");
            receiptIdNode.InnerText = request.ReceiptId;
            root.AppendChild(receiptIdNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.GetCompletedPayDetailInfo(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetCompletedPayDetailInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("GetCompletedPayDetailInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetCompletedPayDetailInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.OEOrdItemList = new List<GetCompletedPayDetailInfoResponse.OEOrdItemInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/OEOrdItems/OEOrdItem");
            if (nodes != null)
            {
                foreach (XmlNode xNode in nodes)
                {
                    var OEOrdItemInfoObj = new GetCompletedPayDetailInfoResponse.OEOrdItemInfo();
                    var InsuType = xNode.SelectSingleNode("InsuType")?.InnerText ?? "";
                    var OEOrdId = xNode.SelectSingleNode("OEOrdId")?.InnerText ?? "";
                    var ArcmiDesc = xNode.SelectSingleNode("ArcmiDesc")?.InnerText ?? "";
                    var Price = xNode.SelectSingleNode("Price")?.InnerText ?? "";
                    var Qty = xNode.SelectSingleNode("Qty")?.InnerText ?? "";
                    var UOM = xNode.SelectSingleNode("UOM")?.InnerText ?? "";
                    var TotalAmount = xNode.SelectSingleNode("TotalAmount")?.InnerText ?? "";
                    var PatientShareAmt = xNode.SelectSingleNode("PatientShareAmt")?.InnerText ?? "";
                    var RecLoc = xNode.SelectSingleNode("RecLoc")?.InnerText ?? "";
                    var RecAddress = xNode.SelectSingleNode("RecAddress")?.InnerText ?? "";

                    OEOrdItemInfoObj.NameDesc = ArcmiDesc;
                    OEOrdItemInfoObj.ItemPrice = Price;
                    OEOrdItemInfoObj.Unit = UOM;
                    OEOrdItemInfoObj.Qty = Qty;
                    OEOrdItemInfoObj.Account = TotalAmount;
                    OEOrdItemInfoObj.RecLoc = RecLoc;
                    OEOrdItemInfoObj.RecAddress = RecAddress;

                    response.OEOrdItemList.Add(OEOrdItemInfoObj);

                }
            }

            return response;
        }

        /// <summary>
        /// 获取缴费单状态
        /// </summary>
        [HttpPost]
        [ActionName("GetPayStatus")]
        public GetPayStatusResponse GetPayStatus(GetPayStatusRequest request)
        {
            var wsClient = WsClientFactory.GetHisWsSoapClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode tradeCodeNode = xmlDoc.CreateElement("TradeCode");
            tradeCodeNode.InnerText = "4907";
            root.AppendChild(tradeCodeNode);

            XmlNode cardNoNode = xmlDoc.CreateElement("PatientID");
            cardNoNode.InnerText = request.CardNo;
            root.AppendChild(cardNoNode);

            XmlNode BankTradeInfoNode = xmlDoc.CreateElement("BankTradeInfo");
            BankTradeInfoNode.InnerText = request.TransNo;
            root.AppendChild(BankTradeInfoNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var responseXml = "";
            try
            {
                responseXml = wsClient.CheckOPBillCharge(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetPayStatus 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("GetPayStatus", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var response = new GetPayStatusResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultContent");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            
            return response;
        }

        ///// <summary>
        ///// 获取挂号费用       
        ///// </summary>
        //[HttpPost]
        //[ActionName("GetRegistFee")]
        //public GetRegistFeeResponse GetRegistFee(GetRegistFeeRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
        //    cardNoNode.InnerText = request.CardNo;
        //    root.AppendChild(cardNoNode);

        //    XmlNode regTypeNode = xmlDoc.CreateElement("Regtype");
        //    regTypeNode.InnerText = request.RegType;
        //    root.AppendChild(regTypeNode);

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    var responseXml = "";

        //    try
        //    {
        //        responseXml = wsClient.GetRegistFee(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 GetRegistFee 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("GetRegistFee", requestXml, responseXml);

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var response = new GetRegistFeeResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    if (resultCode != "0")
        //    {
        //        response.ResultCode = resultCode;
        //        response.ResultDesc = resultDesc;
        //        return response;
        //    }

        //    response.ResultCode = resultCode;
        //    response.ResultDesc = resultDesc;

        //    node = xmlDoc.SelectSingleNode("/Response/Fee");
        //    var regFee = node?.InnerText ?? "";
        //    response.RegFee = regFee;

        //    node = xmlDoc.SelectSingleNode("/Response/TreatFee");
        //    var treatFee = node?.InnerText ?? "";
        //    response.TreatFee = treatFee;

        //    return response;
        //}

        /// <summary>
        /// 获取检验、检查单列表
        /// </summary>
        /// <param name="request"></param>
        [HttpPost]
        [ActionName("GetExamInfo")]
        public GetExamInfoResponse GetExamInfo(GetExamInfoRequest request)
        {
            var wsClient = WsClientFactory.GetReportServiceClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode funNameNode = xmlDoc.CreateElement("FunName");
            funNameNode.InnerText = "GetInspectList";
            root.AppendChild(funNameNode);

            xmlDoc.AppendChild(root);
            
            var requestXml = xmlDoc.InnerXml;
            var FunNameInfo = "<Params><Param Name='outpatient_id' Value='"+ request.CardNo+"'/></Params></Request>";
            requestXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + requestXml.Replace("</Request>", FunNameInfo);

            Logger.ErrorFormat("调用 GetExamInfo 失败1，信息：{0}", requestXml);

            var responseXml = "";
            try
            {
                responseXml = wsClient.XHLisWebserviceFun(requestXml);
                wsClient.Close();
            }
            catch (CommunicationException ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetExamInfo 失败3，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetExamInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }
            HospWsLogHelper.Log("GetExamInfo", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var responseObj = new GetExamInfoResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultMsg");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "1")
            {
                responseObj.ResultCode = resultCode;
                responseObj.ResultDesc = resultDesc;
                return responseObj;
            }

            responseObj.ResultCode = resultCode;
            responseObj.ResultDesc = resultDesc;
            responseObj.ExamList = new List<GetExamInfoResponse.ExamInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/Items/Item");
            if (nodes != null)
                foreach (XmlNode xNode in nodes)
                {
                    var examInfoObj = new GetExamInfoResponse.ExamInfo();
                    var examId = xNode.SelectSingleNode("INSPECTION_ID")?.InnerText ?? "";
                    var status = xNode.SelectSingleNode("INSPECTION_STATE")?.InnerText ?? "";
                    var examName = xNode.SelectSingleNode("TEST_ORDER_NAME")?.InnerText ?? "";

                    //医院留样报告不显示给用户
                    if (examName.Contains("留样"))
                    {
                        continue;
                    }
                    
                    examInfoObj.ExamID = examId;
                    examInfoObj.ExamName = examName;
                    examInfoObj.Status = status;

                    responseObj.ExamList.Add(examInfoObj);
                }

            return responseObj;
        }


        /// <summary>
        /// 获取检验单、检查单明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("GetExamDetail")]
        public GetExamDetailResponse GetExamDetail(GetExamDetailRequest request)
        {
            var wsClient = WsClientFactory.GetReportServiceClient();
            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode funNameNode = xmlDoc.CreateElement("FunName");
            funNameNode.InnerText = "GetInspectResult";
            root.AppendChild(funNameNode);

            //XmlNode examIdNode = xmlDoc.CreateElement("Params");
            //examIdNode.InnerText = request.ExamId;
            //root.AppendChild(examIdNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;

            var ParansInfo = "<Params><Param Name='inspection_id' Value='"+request.ExamId+"'/></Params></Request>";
            requestXml = requestXml.Replace("</Request>", ParansInfo);

            var responseXml = "";
            try
            {
                responseXml = wsClient.XHLisWebserviceFun(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetExamDetail 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("GetExamDetail", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var responseObj = new GetExamDetailResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultMsg");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "1")
            {
                responseObj.ResultCode = resultCode;
                responseObj.ResultDesc = resultDesc;
                return responseObj;
            }

            responseObj.ResultCode = resultCode;
            responseObj.ResultDesc = resultDesc;
            responseObj.ExamItemList = new List<GetExamDetailResponse.ExamItemInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/Items/Item");
            if (nodes != null)
                foreach (XmlNode xNode in nodes)
                {
                    var examItemInfoObj = new GetExamDetailResponse.ExamItemInfo();
                    var itemNo = xNode.SelectSingleNode("INSPECTION_ID")?.InnerText ?? "";
                    var labRepItemName = xNode.SelectSingleNode("CHINESE_NAME")?.InnerText ?? "";
                    var labRepResult = xNode.SelectSingleNode("QUANTITATIVE_RESULT")?.InnerText ?? "";
                    var labRepUnits = xNode.SelectSingleNode("TEST_ITEM_REFERENCE")?.InnerText ?? "";

                    //var itemDate = xNode.SelectSingleNode("ItemDate")?.InnerText ?? "";
                    //var examItemName = xNode.SelectSingleNode("ExamItemName")?.InnerText ?? "";
                    //var prompt = xNode.SelectSingleNode("Prompt")?.InnerText ?? "";
                    //var content = xNode.SelectSingleNode("Content")?.InnerText ?? "";
                    //var labRepItemNo = xNode.SelectSingleNode("LabRepItemNo")?.InnerText ?? "";
                    //var labRepItemName = xNode.SelectSingleNode("LabRepItemName")?.InnerText ?? "";
                    //var labInd = xNode.SelectSingleNode("LabInd")?.InnerText ?? "";
                    //var labContext = xNode.SelectSingleNode("LabContext")?.InnerText ?? "";

                    examItemInfoObj.ItemNo = itemNo;
                    examItemInfoObj.LabRepResult = labRepResult;
                    examItemInfoObj.LabRepUnits = labRepUnits;
                    examItemInfoObj.LabRepItemName = labRepItemName;
                    //examItemInfoObj.ItemDate = itemDate;
                    //examItemInfoObj.ExamItemName = examItemName;
                    //examItemInfoObj.Prompt = prompt;
                    //examItemInfoObj.Content = content;
                    //examItemInfoObj.LabItemName = labItemName;
                    //examItemInfoObj.LabRepItemNo = labRepItemNo;
                   
                    //examItemInfoObj.LabContext = labContext;

                    responseObj.ExamItemList.Add(examItemInfoObj);
                }

            return responseObj;
        }

        /// <summary>
        /// 获取检验单、检查单 【药敏】明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("GetExamYmDetail")]
        public GetExamDetailResponse GetExamYmDetail(GetExamDetailRequest request)
        {
            var wsClient = WsClientFactory.GetReportServiceClient();
            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("Request");

            XmlNode funNameNode = xmlDoc.CreateElement("FunName");
            funNameNode.InnerText = "GetInspectYmResult";
            root.AppendChild(funNameNode);

            //XmlNode examIdNode = xmlDoc.CreateElement("Params");
            //examIdNode.InnerText = request.ExamId;
            //root.AppendChild(examIdNode);

            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;
            var FunNameInfo = "<Params><Param Name='inspection_id' Value='" + request.ExamId + "'/></Params></Request>";
            requestXml = requestXml.Replace("</Request>", FunNameInfo);

            var responseXml = "";
            try
            {
                responseXml = wsClient.XHLisWebserviceFun(requestXml);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetExamYmDetail 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            HospWsLogHelper.Log("GetExamYmDetail", requestXml, responseXml);

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseXml);

            var responseObj = new GetExamDetailResponse();

            var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
            var resultCode = node?.InnerText ?? "";

            node = xmlDoc.SelectSingleNode("/Response/ResultMsg");
            var resultDesc = node?.InnerText ?? "";

            if (resultCode != "1")
            {
                responseObj.ResultCode = resultCode;
                responseObj.ResultDesc = resultDesc;
                return responseObj;
            }

            responseObj.ResultCode = resultCode;
            responseObj.ResultDesc = resultDesc;
            responseObj.ExamItemList = new List<GetExamDetailResponse.ExamItemInfo>();
            var nodes = xmlDoc.SelectNodes("/Response/Items/Item");
            if (nodes != null)
                foreach (XmlNode xNode in nodes)
                {
                    var examItemInfoObj = new GetExamDetailResponse.ExamItemInfo();
                    var itemNo = xNode.SelectSingleNode("MICRO_DATA_ID_NAME")?.InnerText ?? "";
                    var labRepItemName = xNode.SelectSingleNode("MICRO_DATA_ID_NAME")?.InnerText ?? "";
                    var labRepResult = xNode.SelectSingleNode("RESULT")?.InnerText ?? "";
                    var labContext = xNode.SelectSingleNode("MICRO_DATA_ID2_RESULT1")?.InnerText ?? "";
                    var name = xNode.SelectSingleNode("MICRO_DATA_ID2_NAME")?.InnerText ?? "";

                    //var itemDate = xNode.SelectSingleNode("ItemDate")?.InnerText ?? "";
                    //var examItemName = xNode.SelectSingleNode("ExamItemName")?.InnerText ?? "";
                    //var prompt = xNode.SelectSingleNode("Prompt")?.InnerText ?? "";
                    //var content = xNode.SelectSingleNode("Content")?.InnerText ?? "";
                    //var labRepItemNo = xNode.SelectSingleNode("LabRepItemNo")?.InnerText ?? "";
                    //var labRepItemName = xNode.SelectSingleNode("LabRepItemName")?.InnerText ?? "";
                    //var labInd = xNode.SelectSingleNode("LabInd")?.InnerText ?? "";


                    examItemInfoObj.ItemNo = itemNo;
                    examItemInfoObj.LabRepResult = labRepResult;
                    examItemInfoObj.LabRepItemName = labRepItemName;
                    examItemInfoObj.LabContext = labContext;
                    examItemInfoObj.Name = name;
                    //examItemInfoObj.ItemDate = itemDate;
                    //examItemInfoObj.ExamItemName = examItemName;
                    //examItemInfoObj.Prompt = prompt;
                    //examItemInfoObj.Content = content;
                    //examItemInfoObj.LabItemName = labItemName;
                    //examItemInfoObj.LabRepItemNo = labRepItemNo;
                    //examItemInfoObj.LabRepUnits = labRepUnits;

                    responseObj.ExamItemList.Add(examItemInfoObj);
                }

            return responseObj;
        }


        ///// <summary>
        ///// 获取用户住院历史信息
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ActionName("GetInPatientHis")]
        //public GetInPatientHisResponse GetInPatientHis(GetInPatientHisRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
        //    cardNoNode.InnerText = request.CardNo;
        //    root.AppendChild(cardNoNode);

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    var responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.GetInPatientHis(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 GetInPatientHis 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("GetInPatientHis", requestXml, responseXml);

        //    responseXml = responseXml.Replace("<VisitId><", "</VisitId><");
        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var responseObj = new GetInPatientHisResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    if (resultCode == "0")
        //    {
        //        responseObj.InPatientHisList = new List<GetInPatientHisResponse.InPatientHis>();
        //        var nodes = xmlDoc.SelectNodes("/Response/InPatientHisList/InPatientHis");

        //        if (nodes != null)
        //        {
        //            foreach (XmlNode xNode in nodes)
        //            {
        //                var inPatientHisObj = new GetInPatientHisResponse.InPatientHis();
        //                var patientId = xNode.SelectSingleNode("PatientId")?.InnerText ?? "";
        //                var visitId = xNode.SelectSingleNode("VisitId")?.InnerText ?? "";
        //                var name = xNode.SelectSingleNode("Name")?.InnerText ?? "";
        //                var inDate = xNode.SelectSingleNode("InDate")?.InnerText ?? "";
        //                var isInp = xNode.SelectSingleNode("IsInp")?.InnerText ?? "";

        //                inPatientHisObj.PatientId = patientId;
        //                inPatientHisObj.VisitId = visitId;
        //                inPatientHisObj.Name = name;
        //                inPatientHisObj.InDate = inDate;
        //                inPatientHisObj.IsInp = isInp;
        //                responseObj.InPatientHisList.Add(inPatientHisObj);
        //            }
        //        }
        //    }

        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    return responseObj;
        //}


        ///// <summary>
        ///// 住院清单
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ActionName("GetInpBillList")]
        //public GetInpBillListResponse GetInpBillList(GetInpBillListRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode patientIdNode = xmlDoc.CreateElement("PatientId");
        //    patientIdNode.InnerText = request.PatientId;
        //    root.AppendChild(patientIdNode);

        //    XmlNode visitIdNode = xmlDoc.CreateElement("VisitId");
        //    visitIdNode.InnerText = request.VisitId;
        //    root.AppendChild(visitIdNode);

        //    XmlNode beginDateNode = xmlDoc.CreateElement("BeginDate");
        //    beginDateNode.InnerText = request.BeginDate;
        //    root.AppendChild(beginDateNode);

        //    XmlNode endDateNode = xmlDoc.CreateElement("EndDate");
        //    endDateNode.InnerText = request.EndDate;
        //    root.AppendChild(endDateNode);

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    var responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.GetInpBillList(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 GetInpBillList 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("GetInpBillList", requestXml, responseXml);

        //    responseXml = responseXml.Replace("<Name><", "</Name><");

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var responseObj = new GetInpBillListResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    if (resultCode == "0")
        //    {
        //        responseObj.BillInfoList = new List<GetInpBillListResponse.BillInfo>();
        //        var nodes = xmlDoc.SelectNodes("/Response/BillInfoList/BillInfo");

        //        if (nodes != null)
        //        {
        //            foreach (XmlNode xNode in nodes)
        //            {
        //                var billInfoObj = new GetInpBillListResponse.BillInfo();
        //                var inpNo = xNode.SelectSingleNode("InpNo")?.InnerText ?? "";
        //                var name = xNode.SelectSingleNode("Name")?.InnerText ?? "";
        //                var age = xNode.SelectSingleNode("Age")?.InnerText ?? "";
        //                var gender = xNode.SelectSingleNode("Gender")?.InnerText ?? "";
        //                var inpDept = xNode.SelectSingleNode("InpDept ")?.InnerText ?? "";
        //                var billDate = xNode.SelectSingleNode("BillDate")?.InnerText ?? "";
        //                var isChild = xNode.SelectSingleNode("IsChild")?.InnerText ?? "";
        //                var costed = xNode.SelectSingleNode("Costed")?.InnerText ?? "";

        //                billInfoObj.InpNo = inpNo;
        //                billInfoObj.Name = name;
        //                billInfoObj.Age = age;
        //                billInfoObj.Gender = gender;
        //                billInfoObj.InpDept = inpDept;
        //                billInfoObj.BillDate = billDate;
        //                billInfoObj.Costed = costed;
        //                billInfoObj.IsChild = isChild;
        //                responseObj.BillInfoList.Add(billInfoObj);
        //            }
        //        }
        //    }

        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    return responseObj;
        //}


        ///// <summary>
        ///// 获取分类费用列表
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ActionName("GetCategoryBillList")]
        //public GetCategoryBillListResponse GetCategoryBillList(GetCategoryBillListRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode patientIdNode = xmlDoc.CreateElement("PatientId");
        //    patientIdNode.InnerText = request.PatientId;
        //    root.AppendChild(patientIdNode);

        //    XmlNode visitIdNode = xmlDoc.CreateElement("VisitId");
        //    visitIdNode.InnerText = request.VisitId;
        //    root.AppendChild(visitIdNode);

        //    XmlNode billDateNode = xmlDoc.CreateElement("BillDate");
        //    billDateNode.InnerText = request.BillDate;
        //    root.AppendChild(billDateNode);

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    var responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.GetCategoryBillList(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 GetCategoryBillList 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("GetCategoryBillList", requestXml, responseXml);

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var responseObj = new GetCategoryBillListResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    if (resultCode == "0")
        //    {
        //        responseObj.CategoryBillList = new List<GetCategoryBillListResponse.CategoryBill>();
        //        var nodes = xmlDoc.SelectNodes("/Response/CategoryBillList/CategoryBill");

        //        if (nodes != null)
        //        {
        //            foreach (XmlNode xNode in nodes)
        //            {
        //                var categoryBillObj = new GetCategoryBillListResponse.CategoryBill();
        //                var categoryId = xNode.SelectSingleNode("CategoryId")?.InnerText ?? "";
        //                var categoryName = xNode.SelectSingleNode("CategoryName")?.InnerText ?? "";
        //                var categoryFee = xNode.SelectSingleNode("CategoryFee")?.InnerText ?? "";

        //                categoryBillObj.CategoryId = categoryId;
        //                categoryBillObj.CategoryName = categoryName;
        //                categoryBillObj.CategoryFee = categoryFee;
        //                responseObj.CategoryBillList.Add(categoryBillObj);
        //            }
        //        }
        //    }

        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;
        //    return responseObj;
        //}

        ///// <summary>
        ///// 获取住院分类费用明细
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ActionName("GetCategoryBillDetail")]
        //public GetCategoryBillDetailResponse GetCategoryBillDetail(GetCategoryBillDetailRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode patientIdNode = xmlDoc.CreateElement("PatientId");
        //    patientIdNode.InnerText = request.PatientId;
        //    root.AppendChild(patientIdNode);

        //    XmlNode visitIdNode = xmlDoc.CreateElement("VisitId");
        //    visitIdNode.InnerText = request.VisitId;
        //    root.AppendChild(visitIdNode);

        //    XmlNode billDateNode = xmlDoc.CreateElement("BillDate");
        //    billDateNode.InnerText = request.BillDate;
        //    root.AppendChild(billDateNode);

        //    XmlNode categoryIdNode = xmlDoc.CreateElement("CategoryId");
        //    categoryIdNode.InnerText = request.CategoryId;
        //    root.AppendChild(categoryIdNode);

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    var responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.GetCategoryBillDetail(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 GetCategoryBillDetailResponse 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("GetCategoryBillDetailResponse", requestXml, responseXml);

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var responseObj = new GetCategoryBillDetailResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    if (resultCode == "0")
        //    {
        //        responseObj.ItemList = new List<GetCategoryBillDetailResponse.ItemInfo>();
        //        var nodes = xmlDoc.SelectNodes("/Response/ItemList/ItemInfo");

        //        if (nodes != null)
        //        {
        //            foreach (XmlNode xNode in nodes)
        //            {
        //                var itemInfoObj = new GetCategoryBillDetailResponse.ItemInfo();
        //                var patientId = xNode.SelectSingleNode("PatientId")?.InnerText ?? "";
        //                var visitId = xNode.SelectSingleNode("VisitId")?.InnerText ?? "";
        //                var isChild = xNode.SelectSingleNode("IsChild")?.InnerText ?? "";
        //                var name = xNode.SelectSingleNode("Name")?.InnerText ?? "";
        //                var billDate = xNode.SelectSingleNode("BillDate")?.InnerText ?? "";
        //                var itemCode = xNode.SelectSingleNode("ItemCode")?.InnerText ?? "";
        //                var itemName = xNode.SelectSingleNode("ItemName")?.InnerText ?? "";
        //                var itemSpec = xNode.SelectSingleNode("ItemSpec")?.InnerText ?? "";
        //                var itemUnit = xNode.SelectSingleNode("ItemUnit")?.InnerText ?? "";
        //                var amount = xNode.SelectSingleNode("Amount")?.InnerText ?? "";
        //                var itemPrice = xNode.SelectSingleNode("ItemPrice")?.InnerText ?? "";
        //                var costs = xNode.SelectSingleNode("Costs")?.InnerText ?? "";

        //                itemInfoObj.PatientId = patientId;
        //                itemInfoObj.VisitId = visitId;
        //                itemInfoObj.IsChild = isChild;
        //                itemInfoObj.Name = name;
        //                itemInfoObj.BillDate = billDate;
        //                itemInfoObj.ItemCode = itemCode;
        //                itemInfoObj.ItemName = itemName;
        //                itemInfoObj.ItemSpec = itemSpec;
        //                itemInfoObj.ItemUnit = itemUnit;
        //                itemInfoObj.Amount = amount;
        //                itemInfoObj.ItemPrice = itemPrice;
        //                itemInfoObj.Costs = costs;
        //                responseObj.ItemList.Add(itemInfoObj);
        //            }
        //        }
        //    }

        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    return responseObj;
        //}

        ///// <summary>
        ///// 查询预交金余额
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ActionName("GetIcCardHolderState")]
        //public GetIcCardHolderStateResponse GetIcCardHolderState(GetIcCardHolderStateRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
        //    cardNoNode.InnerText = request.CardNo;
        //    root.AppendChild(cardNoNode);

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    var responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.GetIcCardHolderState(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 GetIcCardHolderState 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("GetIcCardHolderState", requestXml, responseXml);

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var responseObj = new GetIcCardHolderStateResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    if (resultCode == "0")
        //    {
        //        responseObj.PrepayList = new List<GetIcCardHolderStateResponse.PrepayItem>();
        //        var nodes = xmlDoc.SelectNodes("/Response/Prepaylist");

        //        if (nodes != null)
        //        {
        //            foreach (XmlNode xNode in nodes)
        //            {
        //                var prepayInfoObj = new GetIcCardHolderStateResponse.PrepayItem();
        //                var patientId = xNode.SelectSingleNode("PatientId")?.InnerText ?? "";
        //                var name = xNode.SelectSingleNode("Name")?.InnerText ?? "";
        //                var inpNo = xNode.SelectSingleNode("InpNo")?.InnerText ?? "";
        //                var visitId = xNode.SelectSingleNode("VisitId")?.InnerText ?? "";
        //                var inpDept = xNode.SelectSingleNode("InpDept")?.InnerText ?? "";
        //                var amt = xNode.SelectSingleNode("Amt")?.InnerText ?? "";

        //                prepayInfoObj.PatientId = patientId;
        //                prepayInfoObj.Name = name;
        //                prepayInfoObj.InpNo = inpNo;
        //                prepayInfoObj.VisitId = name;
        //                prepayInfoObj.InpDept = visitId;
        //                prepayInfoObj.Amt = amt;
        //                responseObj.PrepayList.Add(prepayInfoObj);
        //            }
        //        }
        //    }

        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    return responseObj;
        //}

        ///// <summary>
        ///// 缴预交金
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ActionName("IcCardTranToPrepay")]
        //public IcCardTranToPrepayResponse IcCardTranToPrepay(IcCardTranToPrepayRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode cardNoNode = xmlDoc.CreateElement("CardNo");
        //    cardNoNode.InnerText = request.CardNo;
        //    root.AppendChild(cardNoNode);

        //    XmlNode transNoNode = xmlDoc.CreateElement("TransNo");
        //    transNoNode.InnerText = request.TransNo;
        //    root.AppendChild(transNoNode);

        //    XmlNode transTimeNode = xmlDoc.CreateElement("TransTime");
        //    transTimeNode.InnerText = request.TransTime;
        //    root.AppendChild(transTimeNode);

        //    XmlNode zjeNode = xmlDoc.CreateElement("Zje");
        //    zjeNode.InnerText = request.Zje;
        //    root.AppendChild(zjeNode);

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    var responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.IcCardTranToPrepay(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 IcCardTranToPrepay 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("IcCardTranToPrepay", requestXml, responseXml);

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var responseObj = new IcCardTranToPrepayResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    return responseObj;
        //}

        ///// <summary>
        ///// 调用后立即返回，HIS触发生成住院用户前一天消费列表，写到中间数据库的InPatientBillNotice表
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ActionName("GetInPatientCostFee")]
        //public GetInPatientCostFeeResponse GetInPatientCostFee(GetInPatientCostFeeRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    xmlDoc.AppendChild(root);

        //    var requestXml = xmlDoc.InnerXml;

        //    string responseXml;
        //    try
        //    {
        //        responseXml = wsClient.GetInPatientCostFee(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 GetInPatientCostFee 失败，信息：{0}", JsonConvert.SerializeObject(ex));
        //        throw;
        //    }

        //    HospWsLogHelper.Log("GetInPatientCostFee", requestXml, responseXml);

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var responseObj = new GetInPatientCostFeeResponse();

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node?.InnerText ?? "";

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node?.InnerText ?? "";

        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    return responseObj;
        //}

        ///// <summary>   
        /////  病人是否可以录入信息
        ///// </summary>
        //[HttpPost]
        //[ActionName("PatsSaveInfo")]
        //public PatsSaveInfoResponse PatsSaveInfo(PatsSaveInfoRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    //入参xml字串
        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode PatientIdNode = xmlDoc.CreateElement("patientid");
        //    PatientIdNode.InnerText = request.PatientId;
        //    root.AppendChild(PatientIdNode);


        //    xmlDoc.AppendChild(root);

        //    //保存参数与返回
        //    string requestXml = xmlDoc.InnerXml;
        //    string responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.patsSaveInfo(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        Logger.ErrorFormat("调用 patsSaveInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

        //        throw;
        //    }

        //    HospWsLogHelper.Log("patsSaveInfo", requestXml, responseXml);
        //    //解析xml 
        //    var responseObj = new PatsSaveInfoResponse();

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node == null ? "" : node.InnerText;

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node == null ? "" : node.InnerText;


        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    return responseObj;
        //}


        ///// <summary>   
        /////  保存住院病人信息
        ///// </summary>
        //[HttpPost]
        //[ActionName("SaveInpInfo")]
        //public SaveInpInfoResponse SaveInpInfo(SaveInpInfoRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    //入参xml字串
        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode PatientIdNode = xmlDoc.CreateElement("patientid");
        //    PatientIdNode.InnerText = request.PatientId;
        //    root.AppendChild(PatientIdNode);
        //    XmlNode OccupationNode = xmlDoc.CreateElement("occupation");
        //    OccupationNode.InnerText = request.Occupation;
        //    root.AppendChild(OccupationNode);
        //    XmlNode IdNoNode = xmlDoc.CreateElement("id_no");
        //    IdNoNode.InnerText = request.IdNo;
        //    root.AppendChild(IdNoNode);

        //    XmlNode NationNode = xmlDoc.CreateElement("nation");
        //    NationNode.InnerText = request.Nation;
        //    root.AppendChild(NationNode);

        //    XmlNode MaritalStatusNode = xmlDoc.CreateElement("marital_status");
        //    MaritalStatusNode.InnerText = request.MaritalStatus;
        //    root.AppendChild(MaritalStatusNode);

        //    XmlNode BirthPlaceNode = xmlDoc.CreateElement("birth_place");
        //    BirthPlaceNode.InnerText = request.BirthPlace;
        //    root.AppendChild(BirthPlaceNode);

        //    XmlNode CitizenshipNode = xmlDoc.CreateElement("citizenship");
        //    CitizenshipNode.InnerText = request.Citizenship;
        //    root.AppendChild(CitizenshipNode);

        //    XmlNode MailingAddrAcNode = xmlDoc.CreateElement("mailing_addr_ac");
        //    MailingAddrAcNode.InnerText = request.MailingAddrAc;
        //    root.AppendChild(MailingAddrAcNode);

        //    XmlNode MailingAddressNode = xmlDoc.CreateElement("mailing_address");
        //    MailingAddressNode.InnerText = request.MailingAddress;
        //    root.AppendChild(MailingAddressNode);

        //    XmlNode MailingAddrSnNode = xmlDoc.CreateElement("mailing_addr_sn");
        //    MailingAddrSnNode.InnerText = request.MailingAddrSn;
        //    root.AppendChild(MailingAddrSnNode);

        //    XmlNode ZipCodeNode = xmlDoc.CreateElement("zip_code");
        //    ZipCodeNode.InnerText = request.ZipCode;
        //    root.AppendChild(ZipCodeNode);

        //    XmlNode PhoneNumberHomeNode = xmlDoc.CreateElement("phone_number_home");
        //    PhoneNumberHomeNode.InnerText = request.PhoneNumberHome;
        //    root.AppendChild(PhoneNumberHomeNode);

        //    XmlNode RegResidenceAcNode = xmlDoc.CreateElement("reg_residence_ac");
        //    RegResidenceAcNode.InnerText = request.RegResidenceAc;
        //    root.AppendChild(RegResidenceAcNode);

        //    XmlNode RegResidenceNode = xmlDoc.CreateElement("reg_residence");
        //    RegResidenceNode.InnerText = request.RegResidence;
        //    root.AppendChild(RegResidenceNode);

        //    XmlNode RegResidenceSnNode = xmlDoc.CreateElement("reg_residence_sn");
        //    RegResidenceSnNode.InnerText = request.RegResidenceSn;
        //    root.AppendChild(RegResidenceSnNode);

        //    XmlNode RegResidenceZipCodeNode = xmlDoc.CreateElement("reg_residence_zip_code");
        //    RegResidenceZipCodeNode.InnerText = request.RegResidenceZipCode;
        //    root.AppendChild(RegResidenceZipCodeNode);

        //    XmlNode UnitNameNode = xmlDoc.CreateElement("unit_name");
        //    UnitNameNode.InnerText = request.UnitName;
        //    root.AppendChild(UnitNameNode);

        //    XmlNode UnitAddrAcNode = xmlDoc.CreateElement("unit_addr_ac");
        //    UnitAddrAcNode.InnerText = request.UnitAddrAc;
        //    root.AppendChild(UnitAddrAcNode);

        //    XmlNode UnitAddressNode = xmlDoc.CreateElement("unit_address");
        //    UnitAddressNode.InnerText = request.UnitAddress;
        //    root.AppendChild(UnitAddressNode);

        //    XmlNode UnitAddrSnNode = xmlDoc.CreateElement("unit_addr_sn");
        //    UnitAddrSnNode.InnerText = request.UnitAddrSn;
        //    root.AppendChild(UnitAddrSnNode);

        //    XmlNode UnitZipCodeNode = xmlDoc.CreateElement("unit_zip_code");
        //    UnitZipCodeNode.InnerText = request.UnitZipCode;
        //    root.AppendChild(UnitZipCodeNode);

        //    XmlNode PhoneNumberBusinessNode = xmlDoc.CreateElement("phone_number_business");
        //    PhoneNumberBusinessNode.InnerText = request.PhoneNumberBusiness;
        //    root.AppendChild(PhoneNumberBusinessNode);

        //    XmlNode NextOfKinNode = xmlDoc.CreateElement("next_of_kin");
        //    NextOfKinNode.InnerText = request.NextOfKin;
        //    root.AppendChild(NextOfKinNode);

        //    XmlNode RelationshipNode = xmlDoc.CreateElement("relationship");
        //    RelationshipNode.InnerText = request.Relationship;
        //    root.AppendChild(RelationshipNode);

        //    XmlNode NextOfKinAddrNode = xmlDoc.CreateElement("next_of_kin_addr");
        //    NextOfKinAddrNode.InnerText = request.NextOfKinAddr;
        //    root.AppendChild(NextOfKinAddrNode);

        //    XmlNode NextOfKinZipCodeNode = xmlDoc.CreateElement("next_of_kin_zip_code");
        //    NextOfKinZipCodeNode.InnerText = request.NextOfKinZipCode;
        //    root.AppendChild(NextOfKinZipCodeNode);

        //    XmlNode NextOfKinPhoneNode = xmlDoc.CreateElement("next_of_kin_phone");
        //    NextOfKinPhoneNode.InnerText = request.NextOfKinPhone;
        //    root.AppendChild(NextOfKinPhoneNode);

        //    XmlNode NativePlaceNode = xmlDoc.CreateElement("native_place");
        //    NativePlaceNode.InnerText = request.NativePlace;
        //    root.AppendChild(NativePlaceNode);


        //    xmlDoc.AppendChild(root);

        //    //保存参数与返回
        //    string requestXml = xmlDoc.InnerXml;
        //    string responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.saveInpInfo(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        HospWsLogHelper.Log("saveInpInfo", requestXml, ex.ToString());
        //        Logger.ErrorFormat("调用 saveInpInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

        //        throw;
        //    }

        //    HospWsLogHelper.Log("saveInpInfo", requestXml, responseXml);
        //    //解析xml 
        //    var responseObj = new SaveInpInfoResponse();

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node == null ? "" : node.InnerText;

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node == null ? "" : node.InnerText;


        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    return responseObj;
        //}
        ///// <summary>   
        /////  获取病人住院登记信息
        ///// </summary>
        //[HttpPost]
        //[ActionName("GetInpInfo")]
        //public GetInpInfoResponse GetInpInfo(GetInpInfoRequest request)
        //{
        //    var wsClient = WsClientFactory.GetHisWsSoapClient();

        //    //入参xml字串
        //    var xmlDoc = new XmlDocument();
        //    XmlNode root = xmlDoc.CreateElement("req");

        //    XmlNode PatientIdNode = xmlDoc.CreateElement("patientid");
        //    PatientIdNode.InnerText = request.PatientId;
        //    root.AppendChild(PatientIdNode);

        //    xmlDoc.AppendChild(root);

        //    //保存参数与返回
        //    string requestXml = xmlDoc.InnerXml;
        //    string responseXml = "";
        //    try
        //    {
        //        responseXml = wsClient.getInpInfo(requestXml);
        //        wsClient.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        wsClient.Abort();
        //        HospWsLogHelper.Log("getInpInfo", requestXml, ex.ToString());
        //        Logger.ErrorFormat("调用 getInpInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

        //        throw;
        //    }

        //    HospWsLogHelper.Log("getInpInfo", requestXml, responseXml);
        //    //解析xml 
        //    var responseObj = new GetInpInfoResponse();

        //    xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(responseXml);

        //    var node = xmlDoc.SelectSingleNode("/Response/ResultCode");
        //    var resultCode = node == null ? "" : node.InnerText;

        //    node = xmlDoc.SelectSingleNode("/Response/ResultContent");
        //    var resultDesc = node == null ? "" : node.InnerText;


        //    responseObj.ResultCode = resultCode;
        //    responseObj.ResultDesc = resultDesc;

        //    if (responseObj.ResultCode == "0")
        //    {
        //        var nodes = xmlDoc.SelectNodes("res/patInfo");
        //        if (nodes == null)
        //        {
        //            responseObj.ResultCode = "-1";
        //            responseObj.ResultDesc = "查询不到信息";
        //        }
        //        else
        //        {
        //            foreach (XmlNode pinfo in nodes)
        //            {
        //                responseObj.PatientId = pinfo["patientid"] == null ? "" : pinfo["patientid"].InnerText;
        //                responseObj.Occupation = pinfo["occupation"] == null ? "" : pinfo["occupation"].InnerText;
        //                responseObj.IdNo = pinfo["id_no"] == null ? "" : pinfo["id_no"].InnerText;
        //                responseObj.Nation = pinfo["nation"] == null ? "" : pinfo["nation"].InnerText;
        //                responseObj.MaritalStatus = pinfo["marital_status"] == null ? "" : pinfo["marital_status"].InnerText;
        //                responseObj.BirthPlace = pinfo["birth_place"] == null ? "" : pinfo["birth_place"].InnerText;
        //                responseObj.Citizenship = pinfo["citizenship"] == null ? "" : pinfo["citizenship"].InnerText;
        //                responseObj.MailingAddrAc = pinfo["mailing_addr_ac"] == null ? "" : pinfo["mailing_addr_ac"].InnerText;
        //                responseObj.MailingAddress = pinfo["mailing_address"] == null ? "" : pinfo["mailing_address"].InnerText;
        //                responseObj.MailingAddrSn = pinfo["mailing_addr_sn"] == null ? "" : pinfo["mailing_addr_sn"].InnerText;
        //                responseObj.ZipCode = pinfo["zip_code"] == null ? "" : pinfo["zip_code"].InnerText;
        //                responseObj.PhoneNumberHome = pinfo["phone_number_home"] == null ? "" : pinfo["phone_number_home"].InnerText;
        //                responseObj.RegResidenceAc = pinfo["reg_residence_ac"] == null ? "" : pinfo["reg_residence_ac"].InnerText;
        //                responseObj.RegResidence = pinfo["reg_residence"] == null ? "" : pinfo["reg_residence"].InnerText;
        //                responseObj.RegResidenceSn = pinfo["reg_residence_sn"] == null ? "" : pinfo["reg_residence_sn"].InnerText;
        //                responseObj.RegResidenceZipCode = pinfo["reg_residence_zip_code"] == null ? "" : pinfo["reg_residence_zip_code"].InnerText;
        //                responseObj.UnitName = pinfo["unit_name"] == null ? "" : pinfo["unit_name"].InnerText;
        //                responseObj.UnitAddrAc = pinfo["unit_addr_ac"] == null ? "" : pinfo["unit_addr_ac"].InnerText;
        //                responseObj.UnitAddress = pinfo["unit_address"] == null ? "" : pinfo["unit_address"].InnerText;
        //                responseObj.UnitAddrSn = pinfo["unit_addr_sn"] == null ? "" : pinfo["unit_addr_sn"].InnerText;
        //                responseObj.UnitZipCode = pinfo["unit_zip_code"] == null ? "" : pinfo["unit_zip_code"].InnerText;
        //                responseObj.PhoneNumberBusiness = pinfo["phone_number_business"] == null ? "" : pinfo["phone_number_business"].InnerText;
        //                responseObj.NextOfKin = pinfo["next_of_kin"] == null ? "" : pinfo["next_of_kin"].InnerText;
        //                responseObj.Relationship = pinfo["relationship"] == null ? "" : pinfo["relationship"].InnerText;
        //                responseObj.NextOfKinAddr = pinfo["next_of_kin_addr"] == null ? "" : pinfo["next_of_kin_addr"].InnerText;
        //                responseObj.NextOfKinZipCode = pinfo["next_of_kin_zip_code"] == null ? "" : pinfo["next_of_kin_zip_code"].InnerText;
        //                responseObj.NextOfKinPhone = pinfo["next_of_kin_phone"] == null ? "" : pinfo["next_of_kin_phone"].InnerText;
        //                responseObj.NativePlace = pinfo["native_place"] == null ? "" : pinfo["native_place"].InnerText;
        //            }
        //        }
        //    }
        //    return responseObj;
        //}

        #endregion
    }

    public class ScheduleInfoComparer : IComparer<GetScheduleInfoResponse.ScheduleInfo>
    {
        public int Compare(GetScheduleInfoResponse.ScheduleInfo x, GetScheduleInfoResponse.ScheduleInfo y)
        {
            var rtn = String.Compare(x.RegDate, y.RegDate, StringComparison.Ordinal);
            if (rtn == 0)
            {
                if (x.TotalCount == "不限号" && y.TotalCount != "不限号")
                {
                    return -1;
                }
                if (x.TotalCount != "不限号" && y.TotalCount == "不限号")
                {
                    return 1;
                }

                if (x.LeaveCount.ToInt32OrDefault() > 0 && y.LeaveCount.ToInt32OrDefault() <= 0)
                {
                    return -1;
                }
                if (x.LeaveCount.ToInt32OrDefault() <= 0 && y.LeaveCount.ToInt32OrDefault() > 0)
                {
                    return 1;
                }

                rtn = String.Compare(x.StartTime, y.StartTime, StringComparison.Ordinal);
                if (rtn == 0)
                {
                    var titles = new[] { "主任医师", "副主任医师", "主治医师", "医师" };

                    rtn = titles.IndexOf(x.Title).CompareTo(titles.IndexOf(y.Title));
                }
            }
            return rtn;
        }
    }
}